package com.jymj.service.impl;

import com.jymj.dao.ComplaintsDao;
import com.jymj.entity.ComplaintsEntity;
import com.jymj.entity.ComplaintsFile;
import com.jymj.entity.dto.ComplaintsDto;
import com.jymj.entity.vo.ComplaintsVo;
import com.jymj.service.ComplaintsService;
import com.jymj.utils.FileUtile.FileUploadEntity;
import com.jymj.utils.RRException;
import com.jymj.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by ${lc} on 2021/6/21.
 */
@Service
public class ComplaintsServiceImpl implements ComplaintsService {
    @Autowired
    private ComplaintsDao complaintsDao;

    @Override
    public List<ComplaintsEntity> queryList(ComplaintsDto complaintsDto) {
        return complaintsDao.queryList(complaintsDto);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void save(ComplaintsEntity complaintsEntity) {
        complaintsEntity.setUserid(ShiroUtils.getUserId());
        complaintsEntity.setCreateTime(new Date());
        int i = complaintsDao.save(complaintsEntity);
        if (i < 1){
            throw new RRException("添加失败");
        }
        if (complaintsEntity.getIds() != null && complaintsEntity.getIds().length > 0){
            complaintsDao.updateFile(complaintsEntity.getIds(),complaintsEntity.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void update(ComplaintsEntity complaintsEntity) {
        int i = complaintsDao.update(complaintsEntity);
        if (i < 1){
            throw new RRException("修改失败");
        }
        if (complaintsEntity.getIds() != null && complaintsEntity.getIds().length > 0){
            complaintsDao.updateFile(complaintsEntity.getIds(),complaintsEntity.getId());
        }
    }

    @Override
    public void delete(Long... ids) {
        int i = complaintsDao.delete(ids);
        if (i < 1){
            throw new RRException("删除失败");
        }
    }

    @Override
    public List<ComplaintsVo> statistical(ComplaintsDto complaintsDto) {
        complaintsDto.setSysUserEntity(ShiroUtils.getUserEntity());
        List<ComplaintsVo> complaintsVos = complaintsDao.statistical(complaintsDto);
        Long zjs = 0L;//总件数
        Long yjjs = 0L;//已解决数
        Long wjjs = 0L;//未解决数
        for (ComplaintsVo complaintsVo : complaintsVos) {
            zjs += complaintsVo.getZjs() == null ? 0L : complaintsVo.getZjs();
            yjjs += complaintsVo.getYjjs() == null ? 0L : complaintsVo.getYjjs();
            wjjs += complaintsVo.getWjjs() == null ? 0L : complaintsVo.getWjjs();
        }
        ComplaintsVo complaintsVo = new  ComplaintsVo();
        complaintsVo.setZhen("总计").setZjs(zjs).setYjjs(yjjs).setWjjs(wjjs);
        complaintsVos.add(0,complaintsVo);
        return complaintsVos;
    }

    @Override
    public ComplaintsFile saveFile(FileUploadEntity fileUploadEntity) {
        ComplaintsFile complaintsFile = new ComplaintsFile();
        complaintsFile.setName(fileUploadEntity.getFileName())
                .setPath(fileUploadEntity.getDataUrl())
                .setMsid(-1)
                .setEntime(new Date())
                .setEnpsn(ShiroUtils.getUserId());
        complaintsDao.saveFile(complaintsFile);
        return complaintsFile;
    }

    @Override
    public void deleteFile(Integer id) {
        complaintsDao.deleteFile(id);
    }

    @Override
    public ComplaintsFile queryObject(Integer id) {
        return complaintsDao.queryObject(id);
    }

    @Override
    public List<ComplaintsFile> queryFiles(Long[] ids) {
        return complaintsDao.queryFiles(ids);
    }
}
